package freezzz.client.EventWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import freezzz.client.EventWidget.TimelineEvent;

public class EventWidget extends AbsolutePanel {
    PopupPanel simplePopup;

    public EventWidget ( TimelineEvent e ) {
        super ();
        this.setStylePrimaryName ( "event-widget" );


        Image icon = new Image ( e.getType ().getIconUrl () );
        icon.setStyleName ( "event-icon" );

        this.add ( icon );
        simplePopup = new PopupPanel ( true );
        simplePopup.setWidth ( "225px" );
        simplePopup.setStyleName ( "EventWidgetPopup" );

        simplePopup.setWidget ( new HTML ( toHtml ( e ) ) );

        addDomHandler ( new ClickHandler() {
            public void onClick ( ClickEvent event ) {
                Widget source = ( Widget ) event.getSource ();
                int left = source.getAbsoluteLeft ();
                int top = source.getAbsoluteTop ();
                simplePopup.setPopupPosition ( left - 3, top );
                simplePopup.show ();
            }
        }, ClickEvent.getType () );
        sinkEvents ( Event.MOUSEEVENTS );

    }

    public String toHtml ( TimelineEvent e ) {
        Image icon = new Image ( e.getType ().getIconUrl () );
        icon.setStyleName ( "event-icon-popup" );
        return "<div class='popupHeader'>"+icon.toString () + e.getTitle () + "<small>@"+e.getHumanTime ()+"</small></div><div class='popupBody'>" + e.getContent () + "</div><div class='popupFooter'></div>";
    }

}
